
/**
* 数据交互
**/

const config = 'PRD'     //环境配置

const baseUrl = { //常量
  QAS:'https://test.qydjk.org/',   //测试地址
  PRD:'https://m.qydjk.org/'    //生产地址
}  

 
function getUrl(con){    
    if(con === 'QAS'){
        return baseUrl.QAS
    }else{
        return baseUrl.PRD
    }
}

//GET数据请求方法
function get(url,data,callback){      
  if (wx.getStorageSync("token")) {
    data.token = wx.getStorageSync("token")
  }
  wx.showLoading({
      title:'稍等，处理中！',
      mask:true   //显示蒙版，防止穿透
  })
  wx.request({
      url:getUrl(config)+url,
      data: data,
      header: { 'Content-Type': 'application/json' },
      method:'GET',
      success:res=>{
          wx.hideLoading()
          if(res.statusCode !== 200){
              wx.showToast({
                  title:'网络不给力！',
                  icon:'loading',
                  duration:2000
              })
          }else{
              callback(res.data)
          }
      },
      fail:failres=>{
          wx.hideLoading()
              wx.showToast({
                  title:'请求失败！',
                  icon:'loading',
                  duration:2000
              })
      },
      complete:comres=>{
          wx.hideLoading()
      }
  })
}


//POST数据请求方法
function post(url,data,callback){
    if (wx.getStorageSync("token")){
      data.token = wx.getStorageSync("token")
    }
    wx.showLoading({
        title:'请稍等……',
        mask:true   //显示蒙版，防止穿透
    })
    wx.request({
      url: getUrl(config) + url,
      data: data,
      header: { 'Content-Type': 'application/json' },
      method: 'POST',
        success:res=>{
            wx.hideLoading()
            if(res.statusCode !== 200){
                wx.showToast({
                    title:'网络不给力！',
                    icon:'loading',
                    duration:2000
                })
            }else{
                callback(res.data)
            }
        },
      fail:failres=>{
          wx.hideLoading()
              wx.showToast({
                  title:'请求失败！',
                  icon:'loading',
                  duration:2000
              })
      },
      complete:comres=>{
          wx.hideLoading()
      }
    })
}

//DELETE数据请求方法
function del(url,data,callback){
    wx.showLoading({
        title:'删除中，请稍等……',
        mask:true   //显示蒙版，防止穿透
    })
    wx.request({
        url:getUrl(config)+url,
        data: data,
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        method:'DELETE',
        success:res=>{
            wx.hideLoading()
            if(res.statusCode !== 200){
                wx.showToast({
                    title:'网络不给力！',
                    icon:'loading',
                    duration:2000
                })
            }else{
                callback(res.data)
            }
        },
      fail:failres=>{
          wx.hideLoading()
              wx.showToast({
                  title:'请求失败！',
                  icon:'loading',
                  duration:2000
              })
      },
      complete:comres=>{
          wx.hideLoading()
      }
    })
}

//PUT数据请求方法
function put(url,data,callback){
    wx.showLoading({
        title:'修改中，请稍等……',
        mask:true   //显示蒙版，防止穿透
    })
    wx.request({
        url:getUrl(config)+url,
        data: data,
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        method:'PUT',
        success:res=>{
            wx.hideLoading()
            if(res.statusCode !== 200){
                wx.showToast({
                    title:'网络不给力！',
                    icon:'loading',
                    duration:2000
                })
            }else{
                callback(res.data)
            }
        },
      fail:failres=>{
          wx.hideLoading()
              wx.showToast({
                  title:'请求失败！',
                  icon:'loading',
                  duration:2000
              })
      },
      complete:comres=>{
          wx.hideLoading()
      }
    })
}

//百度地图-地址名称获取方法
//  post(,
function getAddressName(lon,lat,callback){
    wx.showLoading({
        title:'提交中，请稍等……',
        mask:true   //显示蒙版，防止穿透
    })
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=EZueXnIGhjGOet9icCe8FORGHdTj6a5H&location=' + lat + ',' + lon + '&output=json&coordtype=wgs84ll',
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        success:res=>{
            wx.hideLoading()
            if(res.statusCode !== 200){
                wx.showToast({
                    title:'网络不给力！',
                    icon:'loading',
                    duration:2000
                })
            }else{
              console.log(res.data.result)
              let catchName = res.data.result.addressComponent.city;
              callback(catchName)
            }
        }
    })
}
function getNowTime() {
  let now = new Date();
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let day = now.getDate();
  if (month < 10) {
    month = '0' + month;
  };
  if (day < 10) {
    day = '0' + day;
  };
  //  如果需要时分秒，就放开
  let h = now.getHours();
  let m = now.getMinutes();
  let s = now.getSeconds();
  let formatDate = year + '.' + month + '.' + day + '   ' + h + ':' + m + ':' + s;
  return formatDate;
}

module.exports = {
    get:get,
    post:post,
    del:del,
    put:put,
    getAddressName:getAddressName,
    getNowTime: getNowTime

}